package com.tianpengtech.front.mobile.common.intercepter;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.mobile.common.util.UserUtil;


/**
 *  tokenCheck intercept
 * @author ggd
 */
public class TokenCheckInterceptor implements Interceptor {

    public void intercept(Invocation inv) {
        Controller c=inv.getController();
        String token=c.getPara("token");
        if (StrKit.isBlank(token)){
            c.renderJson(R.fail("token不存在"));
            return;
        }else{
            User user=UserUtil.getCurrentUser(token,c.getHeader("source"));
            if (user==null){
                c.renderJson(R.fail("token不存在"));
                return;
            }else{
                c.setAttr("user",user);
                inv.invoke();
            }
        }
    }
}
